import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {
  DataSourceVO,
  DataSourceForm,
  DataSourceQuery,
  DataSourceDtoQuery,
  DataSourceDto
} from '@/api/sheet/dataSource/types';

/**
 * 查询数据源维护列表
 * @param query
 * @returns {*}
 */

export const listDataSource = (query?: DataSourceQuery): AxiosPromise<DataSourceVO[]> => {
  return request({
    url: '/system/dataSource/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询数据源维护列表，不分页
 * @param query
 * @returns {*}
 */

export const listAllDataSource = (query?: DataSourceQuery): AxiosPromise<DataSourceVO[]> => {
  return request({
    url: '/system/dataSource/listAll',
    method: 'get',
    params: query
  });
};

/**
 * 查询数据源维护详细
 * @param dataSourceId
 */
export const getDataSource = (dataSourceId: string | number): AxiosPromise<DataSourceVO> => {
  return request({
    url: '/system/dataSource/' + dataSourceId,
    method: 'get'
  });
};

/**
 * 新增数据源维护
 * @param data
 */
export const addDataSource = (data: DataSourceForm) => {
  return request({
    url: '/system/dataSource',
    method: 'post',
    data: data
  });
};

/**
 * 修改数据源维护
 * @param data
 */
export const updateDataSource = (data: DataSourceForm) => {
  return request({
    url: '/system/dataSource',
    method: 'put',
    data: data
  });
};

/**
 * 删除数据源维护
 * @param dataSourceId
 */
export const delDataSource = (dataSourceId: string | number | Array<string | number>) => {
  return request({
    url: '/system/dataSource/' + dataSourceId,
    method: 'delete'
  });
};

/**
 * 查询表名是否存在
 * @param tableName
 */
export const checkTableName = (tableName : string | undefined) => {
  return request({
    url: '/system/dataSource/checkTableName' ,
    params: {tableName},
    method: 'post'
  });
};

/**
 * 查询表名字段是否存在
 * @param tableName
 */
export const checkTableField = (tableName : string | undefined, tableId : string | undefined) => {
  return request({
    url: '/system/dataSource/checkTableField' ,
    params: {tableName, tableId},
    method: 'post'
  });
};


/**
 * 查询数据源映射数据
 * @param query
 * @returns {*}
 */
export const getSourceDataList = (query?: DataSourceDtoQuery): AxiosPromise<DataSourceDto[]> => {
  return request({
    url: '/system/dataSource/sourceDataList',
    method: 'get',
    params: query
  });
};
